home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / DCLAP 4j / SeqPups / apps / clustalw.src / util.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-17  |  5.4 KB  |  349 lines  |  [TEXT/R*ch]

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <errno.h>
  5. #include <stdarg.h>
  6. #include <ctype.h>
  7. #include "clustalw.h"
  8.  
  9. /*
  10. *    Prototypes
  11. */
  12.  
  13. void        *ckalloc(size_t);
  14. void         * ckrealloc(void *,size_t);
  15. void         ckfree(void *);
  16. void        fatal(char *,...);
  17. void        error(char *,...);
  18. void        warning(char *,...);
  19. char         *rtrim(char *);
  20. char         *blank_to_(char *);    /* DES change blanks to _ */
  21. char         *upstr(char *);
  22. char         *lowstr(char *);
  23. void         getstr(char *,char *);
  24. double        getreal(char *,double,double,double);
  25. int        getint(char *,int,int,int);
  26. void        do_system(void);
  27. Boolean        linetype(char *,char *);
  28. Boolean     blankline(char *);
  29. void        get_path(char *,char *);
  30.  
  31. /*
  32. *    ckalloc()
  33. *
  34. *    Tries to allocate "bytes" bytes of memory. Exits program if failed.
  35. *    Return value:
  36. *        Generic pointer to the newly allocated memory.
  37. */
  38.  
  39. void *ckalloc(size_t bytes)
  40. {
  41.     register void *ret;
  42.         extern void *calloc (size_t nelem, size_t elsize);
  43.     
  44.     if( (ret = calloc(bytes, sizeof(char))) == NULL)
  45. /*
  46.     if( (ret = malloc(bytes)) == NULL)
  47. */
  48.         fatal("Out of memory\n");
  49.     else
  50.         return ret;    
  51. }
  52.  
  53. /*
  54. *    ckrealloc()
  55. *
  56. *    Tries to reallocate "bytes" bytes of memory. Exits program if failed.
  57. *    Return value:
  58. *        Generic pointer to the re-allocated memory.
  59. */
  60.  
  61. void *ckrealloc(void *ptr, size_t bytes)
  62. {
  63.     register void *ret;
  64.         extern void *realloc (void *ptr, size_t size);
  65.     
  66.     if( (ret = realloc(ptr, bytes)) == NULL)
  67.         fatal("Out of memory\n");
  68.     else
  69.         return ret;    
  70. }
  71.  
  72. /*
  73. *    ckfree()
  74. *
  75. *    Tries to free memory allocated by ckalloc.
  76. *    Return value:
  77. *        None.
  78. */
  79.  
  80. void ckfree(void *ptr)
  81. {
  82.     free(ptr);
  83. }
  84.  
  85.  
  86. /*
  87. *    fatal()
  88. *
  89. *    Prints error msg to stderr and exits.
  90. *    Variadic parameter list can be passed.
  91. *
  92. *    Return values:
  93. *        none
  94. */
  95.  
  96. void fatal( char *msg,...)
  97. {
  98.     va_list ap;
  99.     
  100.     va_start(ap,msg);
  101.     fprintf(stderr,"\n\nFATAL ERROR: ");
  102.     vfprintf(stderr,msg,ap);
  103.     fprintf(stderr,"\n\n");
  104.     va_end(ap);
  105.     exit(1);
  106. }
  107.  
  108. /*
  109. *    error()
  110. *
  111. *    Prints error msg to stderr.
  112. *    Variadic parameter list can be passed.
  113. *
  114. *    Return values:
  115. *        none
  116. */
  117.  
  118. void error( char *msg,...)
  119. {
  120.     va_list ap;
  121.     
  122.     va_start(ap,msg);
  123.     fprintf(stderr,"\n\nERROR: ");
  124.     vfprintf(stderr,msg,ap);
  125.     fprintf(stderr,"\n\n");
  126.     va_end(ap);
  127. }
  128.  
  129. /*
  130. *    warning()
  131. *
  132. *    Prints warning msg to stderr.
  133. *    Variadic parameter list can be passed.
  134. *
  135. *    Return values:
  136. *        none
  137. */
  138.  
  139. void warning( char *msg,...)
  140. {
  141.     va_list ap;
  142.     
  143.     va_start(ap,msg);
  144.     fprintf(stderr,"\n\nWARNING: ");
  145.     vfprintf(stderr,msg,ap);
  146.     fprintf(stderr,"\n\n");
  147.     va_end(ap);
  148. }
  149.  
  150.  
  151. /*
  152. *    rtrim()
  153. *
  154. *    Removes trailing blanks from a string
  155. *
  156. *    Return values:
  157. *        Pointer to the processed string
  158. */
  159.  
  160. char * rtrim(char *str)
  161. {
  162.     register int p;
  163.  
  164.     p = strlen(str) - 1;
  165.     
  166.     while ( isspace(str[p]) )
  167.         p--;
  168.         
  169.     str[p + 1] = EOS;
  170.     
  171.     return str;
  172. }
  173.  
  174.  
  175. /*
  176. *    blank_to_()
  177. *
  178. *    Replace blanks in a string with underscores
  179. *
  180. *       Also replaces , ; : ( or ) with _
  181. *
  182. *    Return value:
  183. *        Pointer to the processed string
  184. */
  185.  
  186. char * blank_to_(char *str)
  187. {
  188.     int i,p;
  189.  
  190.     p = strlen(str) - 1;
  191.     
  192.     for(i=0;i<=p;i++) 
  193.         if(
  194.                      (str[i]==' ') ||
  195.                      (str[i]==';') ||
  196.                      (str[i]==',') ||
  197.                      (str[i]=='(') ||
  198.                      (str[i]==')') ||
  199.                      (str[i]==':') 
  200.                   )
  201.                       str[i] = '_';
  202.     
  203.     return str;
  204. }
  205.  
  206.  
  207. /*
  208. *    upstr()
  209. *
  210. *    Converts string str to uppercase.
  211. *    Return values:
  212. *        Pointer to the converted string.
  213. */
  214.  
  215. char * upstr(char *str)
  216. {
  217.     register char *s = str;
  218.     
  219.     while( *s = toupper(*s) )
  220.         s++;
  221.         
  222.     return str;
  223. }
  224.  
  225. /*
  226. *    lowstr()
  227. *
  228. *    Converts string str to lower case.
  229. *    Return values:
  230. *        Pointer to the converted string.
  231. */
  232.  
  233. char * lowstr(char *str)
  234. {
  235.     register char *s = str;
  236.     
  237.     while( *s = tolower(*s) )
  238.         s++;
  239.         
  240.     return str;
  241. }
  242.  
  243. void getstr(char *instr,char *outstr)
  244. {    
  245.     fprintf(stdout,"%s: ",instr);
  246.     gets(outstr);
  247. }
  248.  
  249. double getreal(char *instr,double minx,double maxx,double def)
  250. {
  251.     int status;
  252.     double ret;
  253.     char line[MAXLINE];    
  254.     
  255.     while(TRUE) {
  256.         fprintf(stdout,"%s (%.1lf-%.1lf)   [%.1lf]: ",instr,minx,maxx,def);
  257.         gets(line);
  258.         status=sscanf(line,"%lf",&ret);
  259.         if(status == EOF) return def;
  260.         if(ret>maxx) {
  261.             fprintf(stderr,"ERROR: Max. value=%.1lf\n\n",maxx);
  262.             continue;
  263.         }
  264.         if(ret<minx) {
  265.             fprintf(stderr,"ERROR: Min. value=%.1lf\n\n",minx);
  266.             continue;
  267.         }
  268.         break;
  269.     }
  270.     return ret;
  271. }
  272.  
  273.  
  274. int getint(char *instr,int minx,int maxx, int def)
  275. {
  276.     int ret,status;
  277.     char line[MAXLINE];    
  278.  
  279.     while(TRUE) {
  280.         fprintf(stdout,"%s (%d..%d)    [%d]: ",
  281.         instr,(pint)minx,(pint)maxx,(pint)def);
  282.         gets(line);
  283.         status=sscanf(line,"%ld",(long)&ret);
  284.         if(status == EOF) return def;
  285.         if(ret>maxx) {
  286.             fprintf(stderr,"ERROR: Max. value=%d\n\n",(pint)maxx);
  287.             continue;
  288.         }
  289.         if(ret<minx) {
  290.             fprintf(stderr,"ERROR: Min. value=%d\n\n",(pint)minx);
  291.             continue;
  292.         }
  293.         break;
  294.     }
  295.     return ret;
  296. }
  297.  
  298. void do_system()
  299. {
  300. #if !defined(MACINTOSH) && !defined(MSWIN)
  301.     char line[MAXLINE];
  302.     getstr("\n\nEnter system command",line);
  303.     if(*line != EOS)
  304.         system(line);
  305.     fprintf(stdout,"\n\n");
  306. #endif
  307. }
  308.  
  309.  
  310. Boolean linetype(char *line,char *code)
  311. {
  312.     return( strncmp(line,code,strlen(code)) == 0 );
  313. }
  314.  
  315. Boolean blankline(char *line)
  316. {
  317.     int i;
  318.  
  319.     for(i=0;line[i]!='\n' && line[i]!=EOS;i++) {
  320.         if( isdigit(line[i]) ||
  321.             isspace(line[i]) ||
  322.             (line[i] == '*') ||
  323.                     (line[i] == '.')) 
  324.             ;
  325.         else
  326.             return FALSE;
  327.     }
  328.     return TRUE;
  329. }
  330.  
  331.  
  332. void get_path(char *str,char *path)
  333. {
  334.     register int i;
  335.     
  336.     strcpy(path,str);
  337.     for(i=strlen(path)-1;i>-1;--i) {
  338.         if(str[i]==DIRDELIM) {
  339.             i = -1;
  340.             break;
  341.         }
  342.         if(str[i]=='.') break;
  343.     }
  344.     if(i<0)
  345.         strcat(path,".");
  346.     else
  347.         path[i+1]=EOS;
  348. }
  349.